package dingding;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;

/**
 * 返回 钉钉部门 dept_id 一级
 * @author ljl
 *
 */
public class DepartMentList {
	public List<String> getDepartMent() throws Exception {
		// 获取服务端接口调用凭证access_token
	    String access_token = AccessTokenUtil.getToken();
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
		OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
		req.setDeptId(1L);
		req.setLanguage("zh_CN");
		OapiV2DepartmentListsubResponse rsp = client.execute(req, access_token);
		
		 // 把json字符串转为json对象
        JSONObject jsonObj = JSONObject.parseObject(rsp.getBody());
        String result = jsonObj.getString("result");
//        System.out.println("部门：" +result);
        // 获取部门dept_id list
        JSONArray arry = JSONObject.parseArray(result);
        ArrayList<String> arrayList=new ArrayList<String>();
        for (Object object : arry) {
        	String deptString = JSONObject.parseObject(object.toString()).getString("dept_id");
        	arrayList.add(deptString);
        	if("37280300".equals(deptString)) {
        		DepartMentSonList departMentSonList = new DepartMentSonList();
        		arrayList.addAll(departMentSonList.getDepartMentSon(deptString));
        	}
		}
		
        
        return arrayList;
		
	}
	
}
